﻿using System;
using System.Device.Gpio;
using System.Threading;

namespace NFApp1
{
    public class RelayUtils
    {
        // 继电器连接的GPIO引脚（可根据实际连接修改）
        private const int RELAY_PIN = 2;  // GPIO2

        // GPIO控制器
        private static GpioController gpio;
        private static GpioPin relayPin;

        // 继电器状态
        private static bool relayState = false;


        /// <summary>
        /// 初始化GPIO
        /// </summary>
        public static void InitializeGpio()
        {
            // 创建GPIO控制器
            gpio = new GpioController();

            // 配置继电器引脚为输出模式
            relayPin = gpio.OpenPin(RELAY_PIN, PinMode.Output);

            // 初始状态设置为关闭（低电平）
            relayPin.Write(PinValue.Low);
            relayState = false;

            Console.WriteLine("GPIO初始化完成");
        }

        /// <summary>
        /// 打开继电器
        /// </summary>
        public static void TurnOnRelay()
        {
            if (relayPin != null)
            {
                relayPin.Write(PinValue.High);
                relayState = true;
                Console.WriteLine("继电器已打开");
            }
        }

        /// <summary>
        /// 关闭继电器
        /// </summary>
        public static void TurnOffRelay()
        {
            if (relayPin != null)
            {
                relayPin.Write(PinValue.Low);
                relayState = false;
                Console.WriteLine("继电器已关闭");
            }
        }

        /// <summary>
        /// 切换继电器状态
        /// </summary>
        public static void ToggleRelay()
        {
            if (relayState)
            {
                TurnOffRelay();
            }
            else
            {
                TurnOnRelay();
            }
        }


        /// <summary>
        /// 获取继电器当前状态
        /// </summary>
        /// <returns>true为开启，false为关闭</returns>
        public static bool GetRelayState()
        {
            return relayState;
        }

        /// <summary>
        /// 定时控制继电器（开启指定时间后自动关闭）
        /// </summary>
        /// <param name="durationMs">开启持续时间（毫秒）</param>
        public static void TimedRelay(int durationMs)
        {
            Console.WriteLine($"定时开启继电器 {durationMs}ms");
            TurnOnRelay();
            Thread.Sleep(durationMs);
            TurnOffRelay();
        }


        /// <summary>
        /// 脉冲控制继电器
        /// </summary>
        /// <param name="pulseCount">脉冲次数</param>
        /// <param name="pulseWidth">脉冲宽度（毫秒）</param>
        /// <param name="interval">脉冲间隔（毫秒）</param>
        public static void PulseRelay(int pulseCount, int pulseWidth, int interval)
        {
            Console.WriteLine($"脉冲控制: {pulseCount}次, 宽度{pulseWidth}ms, 间隔{interval}ms");

            for (int i = 0; i < pulseCount; i++)
            {
                TurnOnRelay();
                Thread.Sleep(pulseWidth);
                TurnOffRelay();

                if (i < pulseCount - 1) // 最后一次不需要间隔
                {
                    Thread.Sleep(interval);
                }
            }
        }


        /// <summary>
        /// 清理GPIO资源
        /// </summary>
        public static void CleanupGpio()
        {
            try
            {
                if (relayPin != null)
                {
                    // 关闭继电器
                    relayPin.Write(PinValue.Low);
                    relayPin.Dispose();
                }

                if (gpio != null)
                {
                    gpio.Dispose();
                }

                Console.WriteLine("GPIO资源已清理");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"清理资源时发生异常: {ex.Message}");
            }
        }
    }


}